package com.emily.test.utils;

import com.emily.test.service.RedisService;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zc
 * @date ：Created in 2019/4/4 19:24
 * @description：
 */
public class HeaderUtil {

    /**
     * 获取登录用户的公司ID
     * @param request
     * @param redisService
     * @return
     */
    public static int getCompanyId(HttpServletRequest request, RedisService redisService) {
        Map<String, Object> userMap=getUserinfo(request, redisService);
        Integer id = (Integer) userMap.get("companyId");
        if (ConstentUtil.isEmpty(id)) {
            return 0;
        }
        return id;
    }

    /**
     * 获取登录用户的公司code
     * @param request
     * @param redisService
     * @return
     */
    public static String getCompanyCode(HttpServletRequest request, RedisService redisService) {
        Map<String, Object> userMap=getUserinfo(request, redisService);
        String code = (String) userMap.get("companyCode");
        if (ConstentUtil.isEmpty(code)) {
            return null;
        }
        return code;
    }

    /**
     * 获取用户基本信息
     * @param request
     * @param redisService
     * @return
     */
    public static Map<String, Object>  getUserinfo(HttpServletRequest request, RedisService redisService) {
        String token = request.getHeader("token");
        System.out.println("token====" + token);
        Map<String, Object> userMap = (Map<String, Object>) redisService.get(token);
        return userMap;
    }

    /**
     * 获取用户的角色集合
     * @param request
     * @param redisService
     * @return
     */
    public static List<Map<String, Object>> getUserRoleList(HttpServletRequest request, RedisService redisService) {
        Map<String, Object> userMap=getUserinfo(request, redisService);
        List<Map<String, Object>> roleMap=new ArrayList<Map<String, Object>>();
        if(!ConstentUtil.isEmpty(userMap)){
            roleMap= (List<Map<String, Object>>) userMap.get("userRoleList");
        }
        return roleMap;
    }

    /**
     * 获取用户的资源集合
     * @param request
     * @param redisService
     * @return
     */
    public static List<Map<String, Object>> getUserResourceList(HttpServletRequest request, RedisService redisService) {
        Map<String, Object> userMap=getUserinfo(request, redisService);
        List<Map<String, Object>> resourceList=new ArrayList<Map<String, Object>>();
        if(!ConstentUtil.isEmpty(userMap)){
            resourceList= (List<Map<String, Object>>) userMap.get("resourceList");
        }
        return resourceList;
    }
}
